var gulp=require("gulp"),
livereload=require("gulp-livereload"),
webserver=require("gulp-webserver"),
sass=require("gulp-ruby-sass"),
uglify=require("gulp-uglify"),
htmlmin = require("gulp-htmlmin"),
concat = require("gulp-concat"),
imagemin = require("gulp-imagemin")
gulp.task("shuaxin",function(){
	return gulp.src("dest").pipe(webserver({
		livereload:true,
		open:true
	}))
})
gulp.task("move",function(){
	return gulp.src("src/**/*.html")
	.pipe(gulp.dest("dest"))
})
gulp.task("sass",function(){
	return sass("src/scss/*.scss",{style:"compressed"})
	.on("error",function(err){
		console.log(err)
	})
	.pipe(gulp.dest("dest/css"))
})
// 压缩html代码
// gulp-htmlmin
gulp.task("htmlmin",function(){
    return gulp.src("src/**/*.html")
    .pipe(htmlmin({
         removeComments: true, // 移除注释
         collapseWhitespace: true // 移除空白内容
    }))
    .pipe(gulp.dest("dest"))
})

// 压缩js代码
// gulp-uglify
gulp.task("jsmin",function(){
    return gulp.src("src/js/*.js")
    .pipe(uglify())
    .pipe( gulp.dest("dest/js"));
})

// 合并第三方库
// gulp-concat
	gulp.task("concat",function(){
    return gulp.src("src/lib/*.js")
    .pipe(concat("lib.js"))
    .pipe(gulp.dest("dest/lib"));
})

// 压缩图片
// gulp-imagemin
gulp.task('image',function(){
    return gulp.src('src/img/*.{png,jpg,gif,icon}')
            .pipe(imagemin({progressive: true}))//压缩
            .pipe(gulp.dest('dest/img'))//输出
})

gulp.task("watch",function(){
	gulp.watch("src/**/*.{html,css,js,jpg,png}",["move"])
	gulp.watch("src/**/*.scss",["sass"])
})
gulp.task("default",["move","sass","htmlmin","jsmin","image","concat","watch","shuaxin"])
